package com.ipc.admin.service.zzcool.impl;

import com.alibaba.excel.EasyExcel;
import com.ipc.admin.service.zzcool.service.CouponDetailsService;
import com.ipc.common.pojo.vo.R;
import com.ipc.common.pojo.zzcool.bo.CouponBo;
import com.ipc.common.pojo.zzcool.dto.CouponDto;
import com.ipc.core.dao.zzcool.CouponDetailsDao;
import com.ipc.core.entity.zzcool.CouponDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Zzcool
 * @create 2023-04-30 19:40
 */
@Service
public class CouponDetailsServiceImpl implements CouponDetailsService {

    @Resource
    private CouponDetailsDao dao;

    @Override
    public R save(CouponDetails couponDetails) {
        if (couponDetails != null) {
            if (dao.insert(couponDetails) > 0) {
                return R.ok("添加成功");
            }
        }
        return R.fail("添加失败");
    }

//    @Override
//    public R updateById(CouponDetails couponDetails) {
//        return null;
//    }

    @Override
    public R delBycid(int cid) {
        if (cid > 0) {
            if (dao.deleteBycid(cid) > 0) {
                return R.ok("删除成功");
            }
        }
        return R.fail("删除失败");
    }

    /**
     *动态查询优惠卷详情信息
     * @param bo 需要动态查询字段的bo类
     * @return 返回查询结果
     */
    @Override
    public R queryAll(CouponBo bo) {
        List<CouponDto> couponDtos = dao.selectAllCoupons(bo.getIsget(), bo.getStaledated(),bo.getCId());
        if (couponDtos.size() > 0) {
            return R.ok(couponDtos);
        }
        return R.fail("查询失败");
    }

    /**
     * 导出 Excel
     */
    @Override
    public void exportData(HttpServletResponse response) throws IOException {
        //1.查询导出的内容
        List<CouponDto> list = dao.selectAllCoupons(null, null,null);
        //2.设置文件下载
        response.setHeader("Content-Disposition", "attachment;filename=CouponDetails_" + System.currentTimeMillis() + ".xlsx");
        //3.生成Excel
        EasyExcel.write(response.getOutputStream(), CouponDto.class).sheet().doWrite(list);
    }


//    @Override
//    public R queryById(int id) {
//        return null;
//    }
}
